<%@ Language=VBScript.Encode %>
<%
	select case Session("FormName")
		case "F19"
			Session("bChucnang")=8
		case "F22"
			Session("bChucnang")=2
		case "F21"
			Session("bChucnang")=9
		case "F23"
			Session("bChucnang")=7
	end select 
%>
<!--#include file="..\CheckSession.inc"-->
<!--#include file="..\RootContent.inc"-->
<%	
'***********T&#237;nh M&#227; Khi flow m&#7899;i &#273;&#432;&#7907;c t&#7841;o*************************
	sSQL="Select * from Workflow order by ID"
	Set RecMa=server.CreateObject("ADODB.recordset")
	RecMa.CursorType=3
	RecMa.CacheSize=5
	RecMa.Open sSQL,Conn	
	if RecMa.RecordCount then
		recMa.MoveLast
	    select Case RecMa("ID")
		case 9
		    ID=RecMa("ID")+1
		case 999
		    ID=RecMa("ID")+1
		case 9999
		    ID=RecMa("ID")+1
		case 99999
		    ID=RecMa("ID")+1
		case else
		    ID=RecMa("ID")
		end select
		ID=ID+1
		select case len(cstr(ID))
			case 1
				ma="0000" & cstr(ID)
			case 2
				ma="000" & cstr(ID)
			Case 3
				ma="00" & cstr(ID)
			Case 4
				ma="0" & cstr(ID)
			case 5
				ma=cstr(ID)
		end select
	else
		ID=1
		ma="00001"
	end if		
	RecMa.Close
	Set REcma=nothing
	'Lay trich yeu
	nguoitao=Session("bMaCB")
	bMaRoot=Session("bMaCB")
	ngaytao=cstr(Date()) &" "& cstr(Time())
	bComm=Request("bComm")
	bhienthi=Request("bhienthi")
	bhienthi="ok"
	bLuongmau=Request("bLuongmau")
	tvMaworkflow=Request("tvMaworkflow")
	Set ClsFun=server.CreateObject("Vinalines.ClsFunction")	
	bMaflownode=ClsFun.Giaima(request("bMaflownode"))
	Set ClsFun=nothing
	nodecha=left(bMaflownode,5)
	if bComm="vtden" then
		IDVanthu=request("TxtID")
		sSQL="Select * from vtden where ID like '"& IDVanthu &"'"
	end if
	if bComm="vtquyetdinh" then
		IDVanthu=request("TxtID")
		sSQL="Select * from vtquyetdinh where ID like '"& IDVanthu &"'"
	end if
	if bComm="vtnoibo" then
		IDVanthu= trim(request("bID"))
		sSQL="Select * from vtnoibo where idnoibo like '"& IDVanthu &"'"
	end if
	if bComm="vtduthao" then
		IDVanthu=request("bID")
		sSQL="Select soghinhan,Trichyeu,ngaytao from vtduthao where IDDuthao like '"& IDVanthu &"'"
	end if
	if bComm="nguoncon" then
		IDVanthu=request("bID")
		sSQL="Select Tenvuviec from workflow where Maworkflow like '"& NodeCha &"'"
	end if
	if bComm<>"tudo" then
		Set RecVT=server.CreateObject("ADODB.Recordset")
		RecVT.CursorType=3
		RecVT.CacheSize=5
		RecVT.Open sSQL,Conn
		if RecVT.RecordCount then
			if bComm="nguoncon" then
				TenVV=RecVT("Tenvuviec")
			else
				TenVV=RecVT("Trichyeu")				
				if bComm="vtden" then
					bNam=year(RecVT("ngaynhan"))					
					bSoCV=RecVT("sogoc")
					bNgayky=RecVT("ngayky")
					bNgaynhan=RecVT("ngaynhan")
					bnguoiky=RecVT("nguoiky")
					bND="<b>Số công văn:</b>"+bSoCV+"<br>"
					bND=bND+"<b>Ngày công văn:</b>  "+cstr(bNgayky)+"<br>"
					bND=bND+"<b>Ngày công văn đến:</b>  "+cstr(bngaynhan)+"<br>"
					bND=bND+"<b>Người ký:</b>  "+bNguoiky+"<br>"
					bND=bND+"<b>Trích yếu:</b>  "+TenVV+"<br>"
				end if
				if bComm="vtquyetdinh" then
					bNam=year(RecVT("ngaydi"))					
					bSoCV=RecVT("soghinhan")
					bNgayky=RecVT("ngayky")
					bNgaynhan=RecVT("ngaydi")
					bnguoiky=RecVT("nguoiky")
					bND="<b>S&#7889; quy&#7871;t &#273;&#7883;nh:</b>"+bSoCV+"<br>"
					bND=bND+"<b>Ngày ký quy&#7871;t &#273;&#7883;nh:</b>  "+cstr(bNgayky)+"<br>"
					bND=bND+"<b>Ngày quy&#7871;t &#273;&#7883;nh &#273;&#7871;n:</b>  "+cstr(bngaynhan)+"<br>"
					bND=bND+"<b>Người ký:</b>  "+bNguoiky+"<br>"
					bND=bND+"<b>Trích yếu:</b>  "+TenVV+"<br>"
				end if
				if bComm="vtnoibo" then
					bNam=year(RecVT("ngayky"))					
					bSoCV=RecVT("soghinhan")
					bNgayky=RecVT("ngayky")
					bnguoiky=RecVT("nguoiky")
					bND="<b>S&#7889; v&#259;n b&#7843;n n&#7897;i b&#7897;:</b>"+bSoCV+"<br>"
					bND=bND+"<b>Ngày ký v&#259;n b&#7843;n n&#7897;i b&#7897;:</b>  "+cstr(bNgayky)+"<br>"
					bND=bND+"<b>Người ký:</b>  "+bNguoiky+"<br>"
					bND=bND+"<b>Trích yếu:</b>  "+TenVV+"<br>"
				end if
				if bComm="vtduthao" then
					bNam=year(RecVT("ngaytao"))					
				end if
			end if
		end if
		RecVT.Close
		Set RecVT=nothing
	end if	
	if bComm="vtden" then
		TentepTEXT=ma & ".VT"
	end if
	if bComm="vtquyetdinh" then
		TentepTEXT=ma & ".VT"
	end if
	if bComm="vtnoibo" then
		TentepTEXT=ma & ".VT"
	end if
	if bComm="vtduthao" then
		TentepTEXT=ma & ".VT"
	end if
	if bComm="nguoncon" then
		TentepTEXT=ma & ".VT"
	end if
	On Error resume next
	Conn.BeginTrans	
	if bComm<>"tudo" then
		Set ObjFile=server.CreateObject("ActiveFile.File")	
		if bComm="vtden" then
			Path1=RootContent & "Upload\"& cstr(bNam) &"\Den\TEXT" & "\" & IDVanthu & ".VT"
		end if
		if bComm="vtquyetdinh" then
			Path1=RootContent & "Upload\"& cstr(bNam) &"\Quyetdinh\TEXT" & "\" & IDVanthu & ".VT"
		end if
		if bComm="vtnoibo" then
			Path1=RootContent & "Upload\"& cstr(bNam) &"\Noibo\TEXT" & "\" & IDVanthu & ".VT"
		end if
		if bComm="vtduthao" then
			Path1=RootContent & "Upload\"& cstr(bNam) &"\Duthao\TEXT"& "\" & IDVanthu & ".VT"
		end if
		if bComm="nguoncon" then
			Path1=RootContent & "Data\Yeucau\TEXT"& "\" & NodeCha & ".VT"
		end if
		Path3=RootContent & "Data\Yeucau\TEXT" & "\" & TentepTEXT	
		ObjFile.Name=Path1
		if ObjFile.Exists() then
			ObjFile.Name=Path1
			ObjFile.Copy(Path3)
			if bComm="nguoncon" then
				if Request("bPhanhoi")="OK" then
					sSQL="Insert Into Workflow(ID,Maworkflow,tenvuviec,nguoitao,ngaytao,tentepxuly,mavt,Tam,nodecha,phanhoi) values("& ID &",'" & ma & "','"& tenvv &"','"& Session("bMaCB") &"','" & ngaytao & "','"& TentepTEXT &"','"& IDVanthu &"',0,'"& bMaflownode &"',1)"
					conn.Execute sSQL
					sSQL="Update flownode set maworkflowcon='"& ma &"',uyquyen=1 where maworkflownode Like '"& bMaflownode &"'"
					conn.Execute sSQL
					'sSQL="Update workflow set phanhoi=1 where maworkflow = '"& left(bMaflownode,5) &"'"
					'conn.Execute sSQL
				else
					sSQL="Insert Into Workflow(ID,Maworkflow,tenvuviec,nguoitao,ngaytao,tentepxuly,mavt,Tam,nodecha,phanhoi) values("& ID &",'" & ma & "','"& tenvv &"','"& Session("bMaCB") &"','" & ngaytao & "','"& TentepTEXT &"','"& IDVanthu &"',0,'"& bMaflownode &"',0)"
					conn.Execute sSQL	
					sSQL="Update flownode set maworkflowcon='"& ma &"',uyquyen=0,trangthaixl='2' where maworkflownode Like '"& bMaflownode &"'"
					conn.Execute sSQL
					'sSQL="Update workflow set phanhoi=0 where maworkflow = '"& left(bMaflownode,5) &"'"	
					'conn.Execute sSQL
					'sSQL="Update flownode set trangthaixl='2' where maworkflownode = '"& bMaflownode &"'"
					'Conn.Execute sSQL
				end if
			else
				sSQL="Insert Into Workflow(ID,Maworkflow,tenvuviec,nguoitao,ngaytao,tentepxuly,mavt,Tam) values("& ID &",'" & ma & "','"& tenvv &"','"& Session("bMaCB") &"','" & ngaytao & "','"& TentepTEXT &"','"& IDVanthu &"',0)"
				conn.Execute sSQL
			end if			
			loi=false
		else
			loi=true
			if bComm="vtden" or bComm="vtquyetdinh" or bComm="vtnoibo" then
				Set ClsF=server.CreateObject("Vinalines.clsFile")
				ClsF.SaveFile bND,Path3
				Set ClsF=nothing
				sSQL="Insert Into Workflow(ID,Maworkflow,tenvuviec,nguoitao,ngaytao,tentepxuly,mavt,Tam) values("& ID &",'" & ma & "','"& tenvv &"','"& Session("bMaCB") &"','" & ngaytao & "','"& TentepTEXT &"','"& IDVanthu &"',0)"
					conn.Execute sSQL
				loi=false
			end if
		end if	
	end if
	if not loi then			
		if bComm<>"tudo" then
			'Copy cac File.VT sang thu nuc workflow		
			Select Case bComm
				Case "vtden"
					sSQL="Select Sothutu,tentep,mota from vanban_Noidung where Mavanthu like '"& IDVanthu &"' and Loaivanthu='1' order by Sothutu"
				Case "vtquyetdinh"
					sSQL="Select Sothutu,tentep,mota from vanban_Noidung where Mavanthu like '"& IDVanthu &"' and Loaivanthu='5' order by Sothutu"
				Case "vtnoibo"
					sSQL="Select Sothutu,tentep,mota from vanban_Noidung where Mavanthu like '"& IDVanthu &"' and Loaivanthu='4' order by Sothutu"
				Case "vtduthao"
					sSQL="Select Sothutu,tentep,mota from vanban_Noidung where Mavanthu like '"& IDVanthu &"' and Loaivanthu='3' order by Sothutu"
				Case "nguoncon"
					sSQL="Select Sothutu,tentep,mota from workflow_Tentep where Maworkflow like '"& NodeCha &"'  order by Sothutu"						
			End select	
			Set RecSTT=server.CreateObject("ADODB.Recordset")		
			RecSTT.CursorType=3
			RecSTT.CacheSize=5	
			RecSTT.Open sSQL,Conn
			STT=0
			Do while not RecSTT.EOF			
				STT=STT+1
				if bComm="vtden" then
					Path1=RootContent & "Upload\"& cstr(bNam) &"\Den\"& RecSTT("Tentep")		
				end if
				if bComm="vtquyetdinh" then
					Path1=RootContent & "Upload\"& cstr(bNam) &"\Quyetdinh\"& RecSTT("Tentep")		
				end if
				if bComm="vtnoibo" then
					Path1=RootContent & "Upload\"& cstr(bNam) &"\Noibo\"& RecSTT("Tentep")		
				end if
				if bComm="vtduthao" then
					Path1=RootContent & "Upload\"& cstr(bNam) &"\Duthao\"& RecSTT("Tentep")
				end if
				if bComm="nguoncon" then
					Path1=RootContent & "Data\Yeucau\"& RecSTT("Tentep")
				end if
				Tentep=ma & "S" & RecSTT("Sothutu") & "." & right(RecSTT("Tentep"),3)
				Path2=RootContent & "Data\Yeucau\"& Tentep
				ObjFile.Name=Path1
				if ObjFile.Exists() then
					ObjFile.Copy(Path2)
					sSQL="Insert into Workflow_tentep(MaWorkflow,sothutu,tentep,mota) values('"& ma &"','"& RecSTT("Sothutu") &"','"& tentep &"','"& RecSTT("mota") &"')"			
					Conn.Execute sSQL
				end if		
				RecSTT.MoveNext
			Loop		
			RecSTT.Close
			set RecSTT=nothing
			Set ObjFile=nothing
			Set ClsFile=nothing
		end if			
	end if
	
	if bComm="tudo" then			
		TentepTEXT=ma & ".VT"
		Set ClsF=server.CreateObject("Vinalines.clsFile")
		Path1=RootContent & "Data\Yeucau\TEXT" & "\" & TentepTEXT
		ClsF.SaveFile "",Path1
		sSQL="Insert Into Workflow(ID,Maworkflow,tenvuviec,nguoitao,ngaytao,tentepxuly,mavt,Tam) values("& ID &",'" & ma & "','"& replace(Request("TxtTrichyeu"),"'","''") &"','"& Session("bMaCB") &"','" & ngaytao & "','"& TentepTEXT &"','"& IDVanthu &"',0)"	
		Conn.Execute sSQL			
		Set ClsF=nothing
		loi=false
	end if
	
	if Err.number then
		Conn.RollbackTrans
	else
		if Session("VanBan")="" then 
			Session("VanBan")="-" & Session("VanBan") & ma & "-"
		else
			Session("VanBan")=Session("VanBan") & ma & "-"
		end if
		Conn.CommitTrans
	end if
%>
<!-- 
'bg thphucluongmau
	if bLuongmau="ok" then
		sSQL="Select * from tvflownode where maworkflow like '"& tvMaworkflow &"' order by maworkflownode"
		Set RecTvLuong=server.CreateObject("ADODB.recordset")
		RecTvLuong.CursorType=3
		RecTvLuong.CacheSize=5
		RecTvLuong.Open sSQL,Conn
		RecTvLuong.MoveFirst
		do while not RecTvLuong.EOF
			ctvnodeid=GepNodeID(RecTvLuong("maworkflownode"),ma)
			if ItemLast(ctvnodeid)=2 then
				sSQL="Insert Into FlowNode(Maworkflownode,Maworkflow,nguoixuly,trangthaixl,hanxuly,yeucauxl,tentepxl,ghichu,IDNguoitao) values ('" & ctvnodeid & "','" & ma & "','" & RecTvLuong("nguoixuly") & "',0,'"& cstr(date()+RecTvLuong("hanxuly"))&" "&cstr(Time()) &"','"& RecTvLuong("yeucauxuly") &"','"& ctvnodeid &".tem','"& RecTvLuong("ykien") &"','"& Session("bMaCB") &"')"
			else
				sSQL="Insert Into FlowNode(Maworkflownode,Maworkflow,nguoixuly,trangthaixl,hanxuly,yeucauxl,tentepxl,ghichu,IDNguoitao) values ('" & ctvnodeid & "','" & ma & "','" & RecTvLuong("nguoixuly") & "',-1,'"& cstr(date()+RecTvLuong("hanxuly"))&" "&cstr(Time()) &"','"& RecTvLuong("yeucauxuly") &"','"& ctvnodeid &".tem','"& RecTvLuong("ykien") &"','"& Session("bMaCB") &"')"
			end if
			Conn.Execute sSQL
			RecTvLuong.MoveNext
		loop
		conn.Close
		set conn=nothing
		RecTvLuong.Close
		set RecTvLuong=nothing
	end if -->
<%	
	if not Loi then
		if bComm="vtden" then
			if bhienthi="ok" then
				lnk="flow.asp?bMaRoot="& bMaRoot &"&bVTDen=ok&IDFlow="& ma &"&Nguoitao="& Nguoitao &"&Filename="&tentep
				Response.Redirect(lnk)
			else
				lnk="../xuly/frmdsvtden.asp"
				Response.Redirect(lnk)
			end if
		end if
		if bComm="vtquyetdinh" then
			if bhienthi="ok" then
				lnk="flow.asp?bMaRoot="& bMaRoot &"&bVTQuyetdinh=ok&IDFlow="& ma &"&Nguoitao="& Nguoitao &"&Filename="&tentep
				Response.Redirect(lnk)
			else
				lnk="../xuly/frmdsvtquyetdinh.asp"
				Response.Redirect(lnk)
			end if
		end if
		if bComm="vtnoibo" then
			if bhienthi="ok" then
				lnk="flow.asp?bMaRoot="& bMaRoot &"&bVTNoibo=ok&IDFlow="& ma &"&Nguoitao="& Nguoitao &"&Filename="&tentep
				Response.Redirect(lnk)
			else
				lnk="../xuly/frmdsvtnoibo.asp"
				Response.Redirect(lnk)
			end if
		end if
		if bComm="vtduthao" then
			if bhienthi="ok" then
				lnk="flow.asp?bMaRoot="& bMaRoot &"&bVTDuthao=ok&IDFlow="& ma &"&Nguoitao="& Nguoitao &"&Filename="&tentep
				Response.Redirect(lnk)
			else
				lnk="../xuly/frmdsvtduthao.asp"
				Response.Redirect(lnk)
			end if
		end if		
		if bComm="tudo" then
			if bhienthi="ok" then
				lnk="flow.asp?bMaRoot="& bMaRoot &"&bTudo=ok&IDFlow="& ma &"&Nguoitao="& Nguoitao &"&Filename="& tentep
				Response.Redirect(lnk)
			else
				lnk="frmdsflow.asp"
				Response.Redirect(lnk)
			end if
		end if
		if bComm="nguoncon" then
			lnk="flow.asp?bMaRoot="& bMaRoot &"&IDFlow="& ma &"&Nguoitao="& Nguoitao &"&Filename="& tentep
			Response.Redirect(lnk)
		end if
	else
		sSQL="Delete from workflow where  ID like "& ID 
		conn.Execute sSQL
		sSQL="Delete from workflow_Tentep where  Maworkflow like "& Ma 
		conn.Execute sSQL
	end if
	
	Err.Clear
	conn.Close
	set conn=nothing	
'ed thphucluongmau
%>
<%if Loi then%>
<html>
<body bgcolor=#F8F9D9 >
	<center><font face=Arial color=red size=4>C&#244;ng v&#259;n n&#224;y kh&#244;ng c&#243; n&#7897;i dung. Kh&#244;ng th&#7875; t&#7841;o &#273;&#432;&#7907;c</font>
		<p><input type=button value="Quay l&#7841;i" name=back style="FONT-family:Arial" LANGUAGE=javascript onclick="return back_onclick()"></p>
	</center>
</body>
</html>
<%end if%>
<SCRIPT ID=clientEventHandlersJS LANGUAGE=javascript>
<!--

function back_onclick() {
	window.history.go(-1);
}

//-->
</SCRIPT>
<script language=VBScript.Encode runat=server>
	' H&#224;m &#273;&#7883;nh d&#7841;ng l&#7841;i ng&#224;y M/D/Y-> D/M/Y		
	Function FormatDate(vDate)
	   If Not IsNull(vDate) Then
	        vM = Right("0" & Month(vDate), 2)
	        vD = Right("0" & Day(vDate), 2)
	        vY = Right("19" & Year(vDate), 4)
	        FormatDate = vD & "/" & vM & "/" & vY
	   End If
	End Function
	Function SaveDate(vDate)
		dim bvitri1
		dim bvitri2
	   vDate=trim(cstr(vDate))
	   bvitri1=instr(1,vDate,"/")
	   bMonth=left(vDate,bvitri1-1)
	   bvitri2=instr(bvitri1+1,vDate,"/")
	   Dim Temp
	   Temp=right(vDate,len(vDate)-bvitri1)
	   bDay=left(Temp,bvitri2-bvitri1-1)
	   bYear=right(vDate,len(vDate)-bvitri2)
       SaveDate=bDay+"/"+bMonth+"/"+bYear
	End Function

	Function ModiString(mess_s)
		message = Replace("" & mess_s,"'", "''")
		ModiString=message
	End function
	Function GepNodeID(tvnodeid,maworkflow)
	GepNodeID=trim(maworkflow)+right(tvnodeid,18)
	End Function
	Function ItemLast(Item)
        Dim i
        Dim It
        Dim vt
        Dim j, Temp
        It = Item
        It = Right(It, Len(It) - 4)
        j = 1
        For i = 2 To Len(It)
            If i < Len(It) Then
                Temp = Mid(It, i, 1)
                i = i + 1
                Temp = Temp & Mid(It, i, 1)
                j = j + 1
                If Temp = "00" Then
                    ItemLast = j - 1
                    Exit For
                Else
                    If i = Len(It) Then
                        ItemLast = j
                    End If
                End If
            End If
        Next
    End Function
</script>